package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.SourceHomepageBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;

import java.util.List;

public class RoomEnviromentAdapter  extends AppAdapter<DeviceBean> {
    public RoomEnviromentAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public RoomEnviromentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RoomEnviromentAdapter.ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final SwitchButton btnHomepageSwitch;
        private final TextView tvEnviromentName;
        private final TextView tvDataSource;

        private ViewHolder() {
            super(R.layout.item_room_enviroment);
            btnHomepageSwitch = findViewById(R.id.btn_homepage_show_switch);
            tvEnviromentName = findViewById(R.id.tv_enviroment_name);
            tvDataSource = findViewById(R.id.tv_data_source);
        }

        @Override
        public void onBindView(int position) {
            DeviceBean item = getItem(position);
            SourceHomepageBean homepageBean = item.getHomepage();
            tvEnviromentName.setText(LLSceneLinkageUtil.getLinkageEnvrionmentName(item.getEnvironmentType()));
            if(homepageBean == null){
                tvDataSource.setText(R.string.not_set);
                btnHomepageSwitch.setChecked(false);
            }else{
                List<DeviceBean> list = homepageBean.getDevices();
                if(list.size() == 0){
                    tvDataSource.setText(R.string.not_set);
                    btnHomepageSwitch.setChecked(false);
                }else{
                    tvDataSource.setText(homepageBean.getDevices().get(0).getName());
                    btnHomepageSwitch.setChecked(Boolean.valueOf(homepageBean.getIsHomePage()));
                }
            }
        }
    }

}
